Skip to content

Speed up Imagick fill with opaque colors - #1560

Merged
olivervogel merged 1 commit into
Intervention:developfrom
javiermarinros:perf/imagick-opaque-fill
Nov 25, 2026
Merged

olivervogel merged 1 commit into
Intervention:developfrom
javiermarinros:perf/imagick-opaque-fill

Conversation

@javiermarinros

Copy link
Copy Markdown
Contributor

FillModifier fills a whole Imagick frame by rasterizing a full size ImagickDraw::rectangle().
For an opaque color every pixel is replaced anyway, so the frame can be reset to the color with
ImagickDraw::color(0, 0, Imagick::PAINT_RESET).
Colors with transparency still use the rectangle, because they must blend with the existing pixels.

CPU per fill('ff5500'), 1920x1280, median of 30 runs:

develop this PR
ImageMagick 7.1.1-43, new canvas 79.3 ms 7.9 ms
ImageMagick 7.1.1-43, decoded JPEG 92.6 ms 19.9 ms
ImageMagick 6.9.11-60, new canvas 56.8 ms 0.9 ms
ImageMagick 6.9.11-60, decoded JPEG 58.6 ms 1.5 ms

Output is identical to develop: pixel signature, alpha channel state, type, colorspace, depth and page
of every frame, frame count, and the PNG (decoded pixels + IHDR), JPEG, GIF and WebP encodings. Checked
on 238 cases (every image in tests/resources, a new canvas, a PNG with partial alpha and grayscale JPEG
and PNG, each filled with 7 colors incl. white, black, primaries and a translucent one), on ImageMagick
6.9.11-60, 6.9.12-98 and 7.1.1-43: 0 differences.

Two tests added: an opaque fill covers transparent and animated images (all frames), and a
translucent fill still blends. Both pass on develop too. The test suite has the same result
before and after (the failures that remain come from extensions missing in my environment: exif, heic).

GD: imagefilledrectangle() is already cheap (4.6 ms at 1200x1200). Turning off alpha blending for
opaque colors saves about 1 ms, so it is not part of this PR.

🤖 Generated with Claude Code

@olivervogel
olivervogel merged commit 5d0a209 into Intervention:develop Nov 25, 2026
8 checks passed
@olivervogel

Copy link
Copy Markdown
Member

Thanks.

@javiermarinros
javiermarinros deleted the perf/imagick-opaque-fill branch November 25, 2026 15:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants